<template>
  <div class="register">
    <!--卡片视图-->
    <el-card class="register_card">
      <!--头部-->
      <template #header>
        <div class="card-header" style="text-align: left">
          <span style="color: blue">商家注册</span>
        </div>
      </template>
      <!--表单-->
      <el-form
        :model="ruleForm"
        :rules="rules"
        label-width="100px"
        size="mini"
        status-icon
        ref="ruleForm"
      >
        <!-- 公司名称 -->
        <el-form-item prop="company" label="公司名称">
          <el-input v-model="ruleForm.company"></el-input>
        </el-form-item>
        <!--公司法人代表-->
        <el-form-item label="法人代表" prop="companyName">
          <el-input
            v-model="ruleForm.companyName"
          ></el-input>
        </el-form-item>
        <!-- 申请人姓名 -->
        <el-form-item label="申请人姓名" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <!--申请人手机号-->
        <el-form-item prop="phone" label="手机号">
          <el-input type="mobile" v-model="ruleForm.phone">
          </el-input>
        </el-form-item>
        <!--验证码-->
        <el-form-item prop="password" label="验证码">
          <el-input
            prefix-icon="el-icon-picture-outline"
            v-model="ruleForm.captcha"
          ></el-input>
          <sidentify :identifyCode="ver"></sidentify>
          <el-link :underline="false" @click.prevent="refresh">刷新验证码</el-link>
        </el-form-item>
      </el-form> 
      <!--尾部-->
      <div class="card-footer">
        <el-divider></el-divider>
        <el-checkbox>我已阅读并同意《认证协议》</el-checkbox><br />
        <el-button style="width: 300px" @click="onregister('ruleForm', $event)"
          >认证</el-button
        >
      </div>
    </el-card>
  </div>
</template>

<script>
import { ref } from "vue";
import { verification } from "@/api/verification.js";
import { auth } from "@/api/register.js";
import Sidentify from "../../components/Sidentify.vue";
export default {
  setup() {
    let ver = ref("");
    function veri() {
      verification().then((res) => {
        ver.value = res.data.data.captcha;
      });
    }
    // function beforeauth() {
    //   if (localStorage.getItem('id')=='')
    //   {
    //     this.$message({
    //             message: "请先登录",
    //             type: "error",
    //           });
    //   }
      
    // }
    // beforeauth();
    veri();
    function refresh() {
      veri();
    }
    return {
      ver,
      refresh,
    };
  },
  components: { Sidentify },
  data() {
    return {
      // 登录验证规则
      ruleForm: {
        company: "",
        companyName: "",
        username: "",
        phone: "",
        userId: "",

      },
      rules: {
        company: [{ required: true, message: "请输入公司名称", trigger: "blur" }],
        companyName: [{ required: true, message: "请输入公司法人代表", trigger: "blur" }],
        username: [{ required: true, message: "请输入申请人姓名", trigger: "blur" }],
        phone: [{ required: true, message: "请输入联系方式", trigger: "blur" }],
        captcha: [{ required: true, message: "请输入验证码", trigger: "blur" }],
      },
    };
  },
  methods: {
    onregister(formName, e) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.ruleForm.userId = localStorage.getItem('id'),
          auth(this.ruleForm)
            .then((res) => {
              console.log(res);
              if (res.data.code === 401) {
                this.$message.error(res.data.msg);
                let target = e.target;
                if (target.nodeName == "SPAN") {
                  target = e.target.parentNode;
                }
                target.blur();
                return;
              }
              this.$message({
                message: "认证成功",
                type: "success",
              });
              setTimeout(() => {
                this.$router.push(
                  {
                    path: "/login",
                  },
                  2000
                );
              });
            })
            .catch((err) => {
              console.log(err);
              this.$message.error("认证失败");
            });
        } else {
          let target = e.target;
          if (target.nodeName == "SPAN") {
            target = e.target.parentNode;
          }
          target.blur();
          return false;
        }
      });
    },
  },
};
</script>

<style scoped>
.register {
  /*背景*/
  /*background: url("../src/assets/backgroud.jepg");*/
  width: 100%;
  padding-top: 50px;
  padding-bottom: 50px;
  margin: 0 auto;
}
.register_card {
  width: 900px;
  margin: 0 auto;
}
.card-footer {
  text-align: center;
}
.register_form {
  width: 800px;
  margin: 0 auto;
}
</style>
